package com.codi.bus.core.service.impl;

import com.codi.base.util.MoneyUtils;
import com.codi.bus.core.dao.PortfolioTradeAccoDao;
import com.codi.bus.core.dao.ProfitDao;
import com.codi.bus.core.domain.Profit;
import com.codi.bus.core.service.ProfitService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: zengxl
 * @Description:
 * @Date: 2018/3/12 9:57
 */
@Service
@Slf4j
public class ProfitServiceImpl implements ProfitService {

    @Autowired
    private ProfitDao profitDao;

    @Autowired
    private PortfolioTradeAccoDao portfolioTradeAccoDao;

    /**
     * 按日期累加用户每日收益
     * @param userId
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public List<Map<String,String>> getUserProfitList(Integer userId, String beginDate, String endDate) {
        List<Map<String,String>> result = Lists.newArrayList();
        List<Profit> profitList = profitDao.selectProfitByUserAndTime(userId,beginDate,endDate);
        if(profitList.isEmpty()){
            return result;
        }
        Map<String,BigDecimal> map = Maps.newHashMap();
        for(Profit profit:profitList){
            if(profit.getProfitValue() == null){
                continue;
            }
            String dateTime = profit.getDateTime();
            BigDecimal total = map.get(dateTime);
            if(total == null){
                total = profit.getProfitValue();
            }else {
                total = MoneyUtils.add(total,profit.getProfitValue());
            }
            map.put(dateTime,total);
        }
        for(String dateTime:map.keySet()){
            Map<String,String> temp = Maps.newHashMap();
            BigDecimal profit = map.get(dateTime);
            temp.put("profitDate",dateTime);
            temp.put("profitValue", profit.setScale(2,BigDecimal.ROUND_HALF_UP).toString());
            result.add(temp);
        }
        result.sort((o1, o2) -> o2.get("profitDate").compareTo(o1.get("profitDate")));
        return result;
    }


    @Override
    public List<Profit> getUserNewProfit(Integer userId, String clientId) {
        List<Profit> profits = profitDao.selectFundCode(userId, clientId);
        List<String> fundCodeList = new ArrayList<>();
        for (Profit profit : profits) {
            if (!fundCodeList.contains(profit.getFundCode())) {
                fundCodeList.add(profit.getFundCode());
            }
        }
        List<Profit> profitReal = new ArrayList<>();
        for (String fundCode : fundCodeList) {
            List<Profit> profit = profitDao.selectAllNewProfit(userId, clientId, fundCode);
            //有份额才加进来
            if (!profit.isEmpty()) {
                for (Profit profit1 : profit) {
                    if (profit1.getShares() != null && profit1.getShares().compareTo(new BigDecimal(0)) != 0) {
                        profitReal.add(profit1);
                    }
                }

            }
        }
        return profitReal;
    }
}
